<template>
	<view class="goods-detail-container">
		<!-- :current="current" -->
		<!-- 详情轮播 -->
		<swiper :indicator-dots=" true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
				<image :src="item.pics_big_url" mode="" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 名字价格等详情信息 -->
		<view class="goods-info-box">
			<view class="goods-price">￥{{goods_info.goods_price}}</view>
			<view class="goods-info-body">
				<view class="goods-name">
					{{goods_info.goods_name}}
				</view>
				<view class="favi">
					<uni-icons type="star" size="18" color="gary"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">
				快递:免运费{{cart.length}}
			</view>
		</view>
		<!-- !!!rich-text里的nodes属性可以在页面中渲染出对应的标签 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!-- uniapp官方组件（商品导航） -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	// 引入辅助函数方法
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex';
	export default {
		components: {},
		// 调用mapstate期间设置一个访问路径（store里面的购物车模块），并通过数组的形式导出成员
		computed: {
			...mapState('m_cart', ['cart']),
			// 将total总数量映射到详情页
			...mapGetters('m_cart', ['total'])
		},
		// 通过监听total值的变化,动态统计商品的数量(为其重新赋值)
		watch: {
			total(newVal) {
				const findResult = this.options.find(x => x.text === '购物车')
				if (findResult) {
					findResult.info = newVal
				}
			}
		},
		data() {
			return {
				goods_info: {},
				// 商品导航左侧按钮组的配置项
				options: [{
					icon: 'shop',
					text: '店铺',
					info: 0,
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 商品导航左侧按钮组的配置项
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			}
		},
		onLoad(options) {
			console.log(options);
			const goods_id = options.goods_id
			this.getGoodsDetail(goods_id)
			// 关键代码
			this.options[1].info = this.total
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			// 获取商品详情的数据
			async getGoodsDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: goods_id
				})
				if (res.meta.status !== 200) {
					uni.$showMsg()
				} else {
					console.log("商品详情数据", res.message);
					this.goods_info = res.message
				}
			},
			// 点击图片预览
			preview(index) {
				// 调用官方预览图片方法
				uni.previewImage({
					// 官方预览图片的索引(索引不显示)
					current: index,
					// 官方需要的图片数组,使用map方法对pics数组进行循环，拿到里面的所有pics_big图片数组
					urls: this.goods_info.pics.map(x => x.pics_big)
				})
			},
			// uniapp官方组件（商品导航）
			onClick(e) {
				console.log(e);
				if (e.content.text === "购物车") {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				console.log(e)
				// this.options[2].info++
				if (e.content.text === "加入购物车") {
					// 定义商品信息对象{goods_id, goods_name,goods_price,goods_count,goods_samll_logo,goods_state}，然后再调用addToCart方法
					const goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_price: this.goods_info.goods_price,
						goods_count: 1,
						goods_small_logo: this.goods_info.goods_small_logo,
						goods_state: true
					}
					// 将定义好的商品信息对象通过此方法传过去
					this.addToCart(goods)
				}
			}
		},


	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;

		swiper {
			height: 23.45rem;

			image {
				height: 100%;
				width: 100%;
			}
		}

		.goods-info-box {
			padding: 10px;

			.goods-price {
				color: darkred;
				font-size: 18px;
				margin: 10px 0;
			}

			.goods-info-body {
				display: flex;
				justify-content: space-between;

				.goods-name {
					margin-right: 10px;
					font-size: 13px;
				}

				.favi {
					display: flex;
					flex-direction: column;
					width: 120px;
					font-size: 12px;
					align-items: center;
					color: gray;
					justify-content: center;
				}
			}

			.yf {
				color: gray;
				font-size: 12px;
				margin: 10px 0;
			}
		}

		.goods_nav {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
		}
	}
</style>
